package com.c.controller.admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.c.model.User;
import com.c.result.SingleResult;
import com.c.service.admin.AdminService;
import com.c.util.RedisUtils;

import cn.hutool.db.Entity;

@Controller
public class AdminCommonController {

	@Autowired
	private AdminService adminService;

	@Autowired
	private RedisUtils redisUtils;

	@RequestMapping("/{jspName}")
	public String showItem(@PathVariable String jspName, HttpServletRequest request) {
		// System.out.println("该用户的session中的 key(user)" +
		// request.getSession().getAttribute("user"));
		return "/admin/" + jspName;
	}

	@RequestMapping("/islogin")
	@ResponseBody
	public SingleResult<Entity> islogin(HttpServletRequest request, HttpServletResponse response, User user) {
		System.out.println(user.getUsername() + "正在请求登陆...");
		SingleResult<Entity> result = adminService.islogin(user);
		if (result.isSuccess()) {
			request.getSession().setAttribute("user", user.getUsername());
		}

		// redisUtils.set("json-test", JSONUtil.parseObj(user).toString(), 100l);
		// System.out.println(redisUtils.getStr("json-test"));
		return result;
	}
}
